package cn.tworice.blog.web;

import cn.tworice.blog.entity.BlogEntry;
import cn.tworice.blog.entity.CountIp;
import cn.tworice.blog.entity.ErrorInfo;
import cn.tworice.blog.service.BlogService;
import cn.tworice.blog.utils.CoreService;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 博客管理模块
 * @author 二饭
 * @email 1786705138@qq.com
 */
@Api(value = "博客管理模块",tags = {"博客管理模块接口"})
@Slf4j
@RestController
@RequestMapping("api/blog")
@CrossOrigin
public class BlogController {

    private BlogService blogService;
    @Autowired
    public void BlogController(BlogService blogService){
        this.blogService=blogService;
    }

    /**
     * 获取博客条目信息
     * 时间、标题、作者、链接、类别标签
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @ApiOperation("获取博客条目信息")
    @GetMapping("getBlogEntry")
    public RequestResult getBlogEntry(HttpServletRequest request){
        return blogService.queryBlogEntryList();
    }

    /**
     * 添加或修改博客条目信息
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @ApiOperation("添加或修改博客条目信息")
    @PostMapping("editBlogEntry")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "如修改，请添加ID",dataType = "int",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "date",value = "日期时间戳",dataType = "long",dataTypeClass = Long.class,required = true),
            @ApiImplicitParam(name = "title",value = "标题,<32",dataType = "string",dataTypeClass = String.class,required = true),
            @ApiImplicitParam(name = "writer",value = "作者,<8",dataType = "string",dataTypeClass = String.class,required = true),
            @ApiImplicitParam(name = "address",value = "文章地址,<128",dataType = "string",dataTypeClass = String.class,required = true),
            @ApiImplicitParam(name = "label",value = "文章标签,<8",dataType = "string",dataTypeClass = String.class,required = false),
    })
    public RequestResult editBlogEntry(@ApiParam("条目信息") BlogEntry blogEntry){

        return blogService.editEntry(blogEntry);
    }

    @ApiOperation("根据ID删除博客条目信息")
    @PostMapping("delEntryById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "博客条目ID",required = true)
    })
    public RequestResult delBlogEntry(Integer id){
        return blogService.delEntryById(id);
    }


    @DeleteMapping("delEntryByIds")
    @ApiOperation("根据ID批量删除博客条目信息")
    @ApiImplicitParam(name = "ids",value = "博客条目ID",required = true,dataType = "Array[Integer]",dataTypeClass = Integer[].class)
    public RequestResult delBlogEntrys(Integer[] ids){
        List<Integer> list = List.of(ids);
        return blogService.delEntryByIds(list);
    }


    /**
     * 验证管理员密码是否正确
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @ApiOperation("管理员登录验证")
    @PostMapping("login")
    @ApiImplicitParam(name = "password",value = "密码",dataType = "string",dataTypeClass = String.class,required = true)
    public RequestResult login(String password, HttpServletRequest request){
        String ipAddress = CoreService.getIpAddress(request);
        return blogService.checkLogin(password,ipAddress);
    }

    /**
     * 判断当前IP是否需要登录
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @ApiOperation("返回当前用户是否需要登录验证")
    @GetMapping("isLogin")
    public RequestResult checkLogin(){
        return new RequestResult(new StatusCode(200,"已登录"));
    }

    @ApiOperation("获取系统错误信息")
    @GetMapping("errorInfo")
    public RequestResult errorInfo(@ApiParam("开始时间") @RequestParam(required = false,defaultValue = "1628900000000") Long startTime,@ApiParam("结束时间") @RequestParam(required = false,defaultValue = "0") Long endTime){
        RequestResult result = new RequestResult();
        result.setCode(new StatusCode(200, "查询成功"));
        // 查询异常信息
        result.appendData("infoList", blogService.getErrorInfo(startTime,endTime));
        // 获取前七天日期
        result.appendData("dateList",blogService.getHistoryDate(7));
        // 获取前七天的异常数 近七日异常最多的接口  近七日异常最多的IP
        Map<String,Object> map=blogService.getErrorSum(7);
        result.appendData("errorSumList",map.get("errorSumList"));
        result.appendData("apiSumData",map.get("apiSumData"));
        result.appendData("ipSumData",map.get("ipSumData"));
        return result;
    }

    @ApiOperation("获取访客记录")
    @GetMapping("userInfo")
    public RequestResult userInfo(){
        RequestResult result = new RequestResult();
        // 获取访客记录
        List<CountIp> list=blogService.getUserInfo();
        // 获取前七天日期
        List<String> dateList=blogService.getHistoryDate(7);
        // 获取前七天IP访问总数
        Map<String,Object> map=blogService.getIpSumByDate(7);

        result.setCode(new StatusCode(200,"查询成功"));
        result.appendData("infoList", list);
        result.appendData("dateList", dateList);
        result.appendData("ipSumList",map.get("ipSum"));
        result.appendData("ipData",map.get("sumByIp"));
        return result;
    }

}
